package com.zitangkou.user.domain.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zitangkou.user.domain.IUserDomainService;
import com.zitangkou.user.domain.UserDomain;
import com.zitangkou.user.domain.converter.IUserDomainConverter;
import com.zitangkou.user.entity.UserDo;
import com.zitangkou.common.enums.StatusEnum;
import com.zitangkou.user.mapper.UserMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Deng Ningning
 * @version 1.0
 * @date 2020/11/13 21:59
 */
@Service
public class UserDomainServiceImpl implements IUserDomainService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private IUserDomainConverter userDomainConverter;

    @Override
    public IPage<UserDomain> selectPageByConditions(Integer current, Integer size, String keyword, StatusEnum status) {
        IPage<UserDo> page = new Page<>(current, size);
        page = userMapper.selectPageByConditions(page, StringUtils.trimToEmpty(keyword), status);
        return new Page<UserDomain>()
                .setRecords(userDomainConverter.toDomainList(page.getRecords()))
                .setTotal(page.getTotal());
    }

    @Override
    public UserDomain getUserById(String userId) {
        return userDomainConverter.toDomain(userMapper.selectById(userId));
    }

    @Override
    public UserDomain selectOneByConditions(UserDomain userDomain) {
        List<UserDo> userDoList = userMapper.selectList(new QueryWrapper<UserDo>()
                .lambda()
                .eq(UserDo::getEnterpriseId, userDomain.getEnterpriseId())
                .eq(StringUtils.isNotBlank(userDomain.getMobile()), UserDo::getMobile, userDomain.getMobile())
                .eq(StringUtils.isNotBlank(userDomain.getUserAccount()), UserDo::getUserAccount, userDomain.getUserAccount())
                .eq(StringUtils.isNotBlank(userDomain.getEmail()), UserDo::getEmail, userDomain.getEmail())
                .eq(UserDo::getStatus, StatusEnum.ENABLED));
        if (CollectionUtils.isEmpty(userDoList)) {
            return null;
        }
        return userDomainConverter.toDomain(userDoList.get(NumberUtils.INTEGER_ZERO));
    }

    @Override
    public boolean updateById(UserDomain userDomain) {
        return userMapper.updateById(userDomainConverter.toDo(userDomain)) > 0;
    }

    @Override
    public boolean insertUser(UserDomain userDomain) {
        return userMapper.insert(userDomainConverter.toDo(userDomain)) > 0;
    }

    @Override
    public int countByConditions(UserDomain userDomain) {
        return userMapper.selectCount(new QueryWrapper<UserDo>()
                .lambda()
                .eq(UserDo::getEnterpriseId, userDomain.getEnterpriseId())
                .eq(StringUtils.isNotBlank(userDomain.getMobile()), UserDo::getMobile, userDomain.getMobile())
                .eq(UserDo::getStatus, StatusEnum.ENABLED)
                .eq(StringUtils.isNotBlank(userDomain.getUserId()), UserDo::getUserId, userDomain.getUserId()));
    }
}
